package guggaTests.tasks
{
	import flexunit.framework.TestCase;
	
	import gugga.tasks.ITask;
	import gugga.tasks.WaitingTask;
	import gugga.tasks.events.TaskEvent;

	public class WaitingTaskTest extends TestCase
	{
		private var delayInterval:Number = 1000;
		private var mTask : ITask;
		
		public function WaitingTaskTest(methodName:String=null)
		{
			super(methodName);
			delayInterval = 1000;
		}
		
		public function testConstructor () : void
		{
			var task : ITask = new WaitingTask(300);
			
			assertNotNull(
				"the constructor should return an instance of the WaitingTask class",
				task
			);
		}
		
		public function testStartAndInterruption() : void
		{
			var task : WaitingTask = new WaitingTask(0.005);
			
			assertFalse(
				"The task must not be running before start",
				task.isRunning()
			) 	
			
			task.start();
			
			assertTrue(
				"The task must be running after start",
				task.isRunning()
			)
			
			task.interrupt(); 	
			
			assertFalse(
				"The task must not be running after interrupt",
				task.isRunning()
			) 
		}
		public function testStartEventDispatching() : void
		{
			var task : WaitingTask = new WaitingTask(300);
			
			task.addEventListener(TaskEvent.START,
				addAsync(verifyRunning, 300,
					new TaskExpectedStateDescriptor("Normal task", task, "start", true)));		
					task.start();
					
		}
		
		public function testInterruptEventDispatching() : void
		{
			var task : WaitingTask = new WaitingTask(0.2);
			
			task.addEventListener(TaskEvent.INTERRUPTED,
				addAsync(verifyRunning, 300,
					new TaskExpectedStateDescriptor("Interrupted task", task, "interrupted", false)));
					task.start();
					task.interrupt();
		}
		
		public function testCompleteEventDispatching() : void
		{
			var task : WaitingTask = new WaitingTask(0.01);
			
			task.addEventListener(TaskEvent.COMPLETED,
				addAsync(verifyRunning, 300,
					new TaskExpectedStateDescriptor("Completed task", task, "completed", false)));
													
			task.start();
		}
		
		public function testEventDispatching() : void
		{
			var interruptedTask : WaitingTask = new WaitingTask(1);
			var completedTask : WaitingTask = new WaitingTask(1);
			
			interruptedTask.addEventListener(TaskEvent.START, 
				addAsync(verifyRunning, 1900, 
					new TaskExpectedStateDescriptor("InterruptedTask", interruptedTask, "started", true)));
			
			interruptedTask.addEventListener(TaskEvent.INTERRUPTED, 
				addAsync(verifyRunning, 2100, 
					new TaskExpectedStateDescriptor("InterruptedTask", interruptedTask, "interupted", false)));

			completedTask.addEventListener(TaskEvent.START, 
				addAsync(verifyRunning, 2200, 
					new TaskExpectedStateDescriptor("CompletedTask", completedTask, "start", true)));
			
			completedTask.addEventListener(TaskEvent.COMPLETED, 
				addAsync(verifyRunning, 2300, 
					new TaskExpectedStateDescriptor("CompletedTask", completedTask, "completed", false)));
			
			assertFalse(
				"interruptedTask have not to be running becouse it isn't started",
				interruptedTask.isRunning()
				);
				
			assertFalse(
				"completedTask have not to be running becouse it isn't started",
				completedTask.isRunning()
				);
				
			interruptedTask.start();
			completedTask.start();
			
			interruptedTask.interrupt();				
		}
		
		private function verifyRunning(aTaskEvent : TaskEvent,
			aTaskExpectedStateDescriptor : TaskExpectedStateDescriptor) : void
		{
			assertEquals(
				 aTaskExpectedStateDescriptor.TaskName + "isRunning have to be not " 
				 	+ aTaskExpectedStateDescriptor.Task.isRunning().toString()  
					+ ", becouse task is " + aTaskExpectedStateDescriptor.TaskExpectedStateString,
				aTaskExpectedStateDescriptor.Task.isRunning(),
				aTaskExpectedStateDescriptor.TaskExpectedState
				)
		}		
	}
}
	import gugga.tasks.ITask;
	

	class TaskExpectedStateDescriptor
	{
		public var TaskName : String;
		public var Task : ITask
		
		public var TaskExpectedStateString : String;
		public var TaskExpectedState : Boolean;

		
		public function TaskExpectedStateDescriptor(aTaskName : String, aTask : ITask,
			aTaskExpectedStateString : String, aTaskExpectedState : Boolean)
		{
			TaskName = aTaskName;
			Task = aTask;
			
			TaskExpectedState = aTaskExpectedState;
			TaskExpectedStateString = aTaskExpectedStateString;
		}
	}